package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.ui.widgets.shopping.AnimShopButton;

/**
 * Created by qiuzhenhuan on 2019/9/24.
 */

public class ADA_CartGoodRedeem extends CommonAdapter<GoodsEntity> {


    public ADA_CartGoodRedeem(Context context) {
        super(context);
    }


    @Override
    protected int itemLayoutId() {
        return R.layout.item_cart_goods_redeem;
    }

    @Override
    protected void convert(ViewHolder holder, final GoodsEntity entity, final int position) {

        holder.setText(R.id.tv_cart_good_name, entity.getGoodsName());

        TextView tvPrice = holder.getView(R.id.tv_cart_good_price);

        tvPrice.setText(String.format("%s积分", Integer.parseInt(entity.getIntegral())));

        holder.setText(R.id.tv_cart_good_spec, getGoodSpecString(entity.getGoodsSpec(), entity.getUnitName()));

        // 删除条目
        holder.setOnClickListener(R.id.iv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onGoodsDeleted(position);
                }
            }
        });

        // 商品数量的加减，需要实时回调
        final AnimShopButton vCartGoodsAmount = holder.getView(R.id.v_cart_goods_amount);
        if (Integer.parseInt(entity.getGoodsBatchNum()) == 1) {
            vCartGoodsAmount.setMaxCount(Integer.parseInt(entity.getGoodsQuantity()));
        }
        vCartGoodsAmount.setCount(StringUtils.isNumber(entity.getGoodsCount()) ? Integer.parseInt(entity.getGoodsCount()) : 0);
        vCartGoodsAmount.setOnAddDelListener(new GoodsShopButtonAdapter(mContext) {
            @Override
            protected void onAmountChanged(int count) {
                if(mListener!=null){
                    mListener.onAmountChanged(entity, vCartGoodsAmount, count);
                }
            }

            @Override
            public void onDelSuccess(int count) {
                entity.setGoodsCount(String.valueOf(count));

                if(mListener!=null){
                    mListener.onAmountChanged(entity, vCartGoodsAmount, count);
                }
            }
        });


    }

    public interface OnGoodsAmountChangedListener {
        void onAmountChanged(GoodsEntity entity, AnimShopButton view, int count);

        void onGoodsDeleted(int count);
    }

    private OnGoodsAmountChangedListener mListener;

    public void setOnGoodsAmountChangedListener(OnGoodsAmountChangedListener mListener) {
        this.mListener = mListener;
    }

    private String getGoodSpecString(List<GoodsEntity.GoodsSpecBean> goodsSpec, String unitName) {
        StringBuilder builder = new StringBuilder();
        builder.append("规格：");
        if (!ListUtil.isListEmpty(goodsSpec) || !TextUtils.isEmpty(unitName)) {
            for (int i = 0; i < goodsSpec.size(); i++) {
                GoodsEntity.GoodsSpecBean goodsSpecBean = goodsSpec.get(i);
                builder.append(goodsSpecBean.getValue());
                builder.append(",");
            }
            builder.append(unitName);
        } else {
            builder.append("暂无");
        }
        return builder.toString();
    }

}
